package com.sicau.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.sicau.common.core.domain.entity.SysRole;
import com.sicau.common.core.domain.entity.SysUser;
import com.sicau.common.core.domain.model.LoginUser;
import com.sicau.common.utils.SecurityUtils;
import com.sicau.common.utils.StringUtils;
import com.sicau.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sicau.common.annotation.Log;
import com.sicau.common.core.controller.BaseController;
import com.sicau.common.core.domain.AjaxResult;
import com.sicau.common.enums.BusinessType;
import com.sicau.system.domain.BLeave;
import com.sicau.system.service.IBLeaveService;
import com.sicau.common.utils.poi.ExcelUtil;
import com.sicau.common.core.page.TableDataInfo;

/**
 * 请假申请Controller
 * 
 * @author ruoyi
 * @date 2022-03-19
 */
@RestController
@RequestMapping("/system/leave")
public class BLeaveController extends BaseController
{
    @Autowired
    private IBLeaveService bLeaveService;

    @Autowired
    private ISysUserService userService;

    /** 管理员角色权限标识 */
    private static final String SUPER_ADMIN = "admin";

    /**
     * 查询请假申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:leave:list')")
    @GetMapping("/list")
    public TableDataInfo list(BLeave bLeave)
    {
        startPage();
        if (hasRole("teacher")) {
            bLeave.setTeacherId(SecurityUtils.getUserId());
        }
        List<BLeave> list = bLeaveService.selectBLeaveList(bLeave);
        return getDataTable(list);
    }

    /**
     * 导出请假申请列表
     */
    @PreAuthorize("@ss.hasPermi('system:leave:export')")
    @Log(title = "请假申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BLeave bLeave)
    {
        if (hasRole("teacher")) {
            bLeave.setTeacherId(SecurityUtils.getUserId());
        }
        List<BLeave> list = bLeaveService.selectBLeaveList(bLeave);
        ExcelUtil<BLeave> util = new ExcelUtil<BLeave>(BLeave.class);
        util.exportExcel(response, list, "请假申请数据");
    }

    /**
     * 获取请假申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:leave:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(bLeaveService.selectBLeaveById(id));
    }

    /**
     * 新增请假申请
     */
    @PreAuthorize("@ss.hasPermi('system:leave:add')")
    @Log(title = "请假申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BLeave bLeave)
    {
        bLeave.setStudentId(SecurityUtils.getUserId());
        bLeave.setCreateBy(SecurityUtils.getUsername());
        return toAjax(bLeaveService.insertBLeave(bLeave));
    }

    /**
     * 修改请假申请
     */
    @PreAuthorize("@ss.hasPermi('system:leave:edit')")
    @Log(title = "请假申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BLeave bLeave)
    {
        bLeave.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(bLeaveService.updateBLeave(bLeave));
    }

    /**
     * 删除请假申请
     */
    @PreAuthorize("@ss.hasPermi('system:leave:remove')")
    @Log(title = "请假申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bLeaveService.deleteBLeaveByIds(ids));
    }


    @PreAuthorize("@ss.hasPermi('system:leave:examine')")
    @Log(title = "请假审核", businessType = BusinessType.UPDATE)
    @PostMapping("/examine")
    public AjaxResult examine(@RequestBody BLeave bLeave){
        BLeave bLeave_new = new BLeave();
        bLeave_new.setId(bLeave.getId());
        bLeave_new.setStatus(bLeave.getStatus());
        return AjaxResult.success(bLeaveService.updateBLeave(bLeave_new));
    }


    @GetMapping("/teacher/teacher")
    public AjaxResult teacherList(String name){
        return AjaxResult.success(userService.teacherList(name));
    }

    /**
     * 判断用户是否拥有某个角色
     *
     * @param role 角色字符串
     * @return 用户是否具备某角色
     */
    private boolean hasRole(String role)
    {
        if (StringUtils.isEmpty(role))
        {
            return false;
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (StringUtils.isNull(loginUser) || CollectionUtils.isEmpty(loginUser.getUser().getRoles()))
        {
            return false;
        }
        for (SysRole sysRole : loginUser.getUser().getRoles())
        {
            String roleKey = sysRole.getRoleKey();
            if (roleKey.equals(StringUtils.trim(role)))
            {
                return true;
            }
        }
        return false;
    }
}
